# -*- coding: utf-8 -*-
import os
import shutil

from src.handler.AbstractHandler import AbstractHandler


class PushDingTalkHandler(AbstractHandler):

    def _setup_arg(self, subparsers):
        parser_s = subparsers.add_parser('dingtalk', help='dingtalk sub help')
        parser_s.add_argument('--replace', '-r', type=bool, default=True, help='replace if file exist')
        parser_s.add_argument('--file', type=str, default='E:\\Maven\\My\\dingtalk\\target\\dingtalk-1.0-SNAPSHOT.jar', help='source file path')
        parser_s.add_argument('--moveTo', type=str, default='D:\\Software\\MyStart\\server', help='destination file path')
        parser_s.set_defaults(func=self.do_process)

    def _check(self, args):
        """检查请求是否为有效的文件路径"""
        return os.path.isfile(args.file)

    def check_fail(self, args):
        """检查请求是否为有效的文件路径"""
        self.logger.warning(f"File {args.file} does not exist.")

    def _process(self, args):
        source = args.file
        destination = args.moveTo

        if os.path.exists(destination) and not args.replace:
            self.logger.warning(f"Destination file {destination} already exists and replace is False. Skipping copy.")
            return

        try:
            shutil.copy(source, destination)
            self.logger.info(f"File copy from {source} to {destination}")
        except Exception as e:
            self.logger.error(f"Failed to move file from {source} to {destination}: {e}")
